<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" style="margin:10px 0px" @click="showDialog">添加</el-button>
    <el-table style="width: 100%" border :data="list">
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="width" align="center">
      </el-table-column>
      <el-table-column prop="prop" label="品牌Logo" width="width" align="center">
        <template slot-scope="{row}">
          <img :src="row.logoUrl" alt="" style="width:100px; height:100px">
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width" align="center">
        <template slot-scope="{row}">
          <el-button type="warning" size="mini" icon="el-icon-edit" @click="editTradeMark(row)">修改</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteTradeMark(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-size="limit" :total="total" :page-sizes="[3, 5, 10]" layout=" prev, pager, next, jumper,->, sizes, total" style="margin-top:20px;text-align:center">
    </el-pagination>

    <!-- 对话框 -->
    <el-dialog :title="this.form.id?'修改品牌':'添加品牌'" :visible.sync="tradeMarkDialog">
      <el-form :rules="rules" ref="ruleform" :model="form">
        <el-form-item label="品牌名称" style="width:80%" prop="tmName">
          <el-input v-model="form.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌Logo" prop="logoUrl">
          <el-upload class="avatar-uploader" action="dev-list/admin/product/fileUpload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="tradeMarkDialog = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateTradeMark">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "TradeMark",
  data() {
    return {
      page: 1,
      limit: 3,
      tradeMarkDialog: false,
      form: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在2-10个字符之间",
            trigger: "change",
          },
        ],
        logoUrl: [{ required: true, message: "请选择品牌的图片" }],
      },
    };
  },
  computed: {
    ...mapState("trademark", {
      tradeMarkList: (state) => state.tradeMarkList,
    }),
    list() {
      return this.tradeMarkList.records;
    },
    total() {
      return this.tradeMarkList.total;
    },
  },
  created() {
    this.getTradeMarkList();
  },
  methods: {
    async getTradeMarkList() {
      let { page, limit } = this;
      this.$store.dispatch("trademark/getTradeMarkList", { page, limit });
    },
    handleSizeChange(size) {
      this.limit = size;
      this.getTradeMarkList();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getTradeMarkList();
    },
    showDialog() {
      this.tradeMarkDialog = true;
      this.form = { tmName: "", logoUrl: "" };
    },
    // 修改品牌
    editTradeMark(row) {
      this.form = { ...row };
      this.tradeMarkDialog = true;
    },
    saveOrUpdateTradeMark() {
      this.$refs.ruleform.validate(async (success) => {
        if (success) {
          let result = null;
          if (this.form.id) {
            result = await this.$API.trademark.reqUpdateTradeMark(this.form);
          } else {
            result = await this.$API.trademark.reqSaveTradeMark(this.form);
          }
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: this.form.id ? "修改成功" : "添加成功",
            });
          } else {
            this.$message({
              type: "warning",
              message: this.form.id ? "修改失败" : "添加失败",
            });
          }
          this.getTradeMarkList(
            this.list.length > 1 ? this.page : this.page - 1
          );
          this.tradeMarkDialog = false;
        } else {
          return false;
        }
      });
    },
    //删除品牌
    deleteTradeMark(row) {
      this.$confirm(`是否确认删除${row.tmName}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        // 发送删除请求
        const result = await this.$API.trademark.reqDeleteTradeMark(row.id);
        console.log(result);
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getTradeMarkList();
        } else {
          this.$message({
            type: "error",
            message: "删除失败",
          });
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.form.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>